
package com.compuvest.table;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class promotionCode extends HttpServlet {
   
    public static Logger log = LoggerFactory.getLogger(promotionCode.class);

    private TableMySQL mysql;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        mysql = new TableMySQL();

        log.info("Init()");
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        String tableID = request.getParameter("id");
        int tableOffset = 0; try { tableOffset = request.getParameter("offset") == null ? 0 : Integer.parseInt(request.getParameter("offset")); } catch(Exception ignore) { }

            StringBuilder xml = new StringBuilder(500);
            xml.append("<?xml version='1.0' encoding='UTF-8'?>\n");
            xml.append("<ajax-response><response type='object' id='shippers_updater'>");
            xml.append("<debug>Query2xml: SQL Dialect=MySQL</debug>");
            xml.append("<debug>Query2xml: numrows=84</debug>");
            xml.append("<debug>Query2xml: resource type=mysql link</debug>");
            xml.append("<rows update_ui='true' offset='0'>");
            xml.append("<tr><td>3</td><td>Federal Shipping</td><td>(503) 555-9931</td></tr>");
            xml.append("<tr><td>1</td><td>Speedy Express</td><td>(503) 555-9831</td></tr>");
            xml.append("<tr><td>2</td><td>United Package</td><td>(503) 555-3199</td></tr>");
            xml.append("</rows>");
            xml.append("<debug>origQuery=SELECT t.ShipperID,t.CompanyName,t.Phone FROM shippers t ORDER BY t.CompanyName ASC</debug>");
            xml.append("<debug>execQuery=SELECT t.ShipperID,t.CompanyName,t.Phone FROM shippers t ORDER BY 2 ASC,t.CompanyName ASC LIMIT 0,85</debug>");
            xml.append("<rowcount>3</rowcount>");
            xml.append("</response></ajax-response>");

//            xml.append(mysql.getAdvertTable(request));

        try(PrintWriter out = response.getWriter()) {
            out.print(xml.toString());
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    } 

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Promotional Code";
    }// </editor-fold>

}
